package com.example.httpclient.controller;

import com.example.httpclient.handler.CustomHttpClientResponseHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.hc.client5.http.classic.methods.HttpGet;
import org.apache.hc.client5.http.config.ConnectionConfig;
import org.apache.hc.client5.http.config.RequestConfig;
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
import org.apache.hc.client5.http.impl.classic.HttpClientBuilder;
import org.apache.hc.client5.http.impl.io.BasicHttpClientConnectionManager;
import org.apache.hc.core5.util.Timeout;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

/**
 * @author zpMeng
 * @since 8/22/2024
 */
@Slf4j
@RestController
@RequestMapping("httpClient")
public class HttpClientController {

    @GetMapping()
    public String test() {
        return "test";
    }

    @GetMapping("durableTask")
    public String testDurableTask(@RequestParam(required = false) Integer seconds) throws InterruptedException {
        if (seconds == null) {
            seconds = 20_000;
        }
        log.info("start");
        Thread.sleep(seconds);
        log.info("finished");
        return "durableTask done";
    }

    @GetMapping("timeoutForDomainAddress")
    public String timeoutForDomainAddress(@RequestParam(required = false) Integer timeout) {
        timeout = timeout == null ? 1000 : timeout * 1000;

        BasicHttpClientConnectionManager connectionManager = new BasicHttpClientConnectionManager();
        connectionManager.setConnectionConfig(ConnectionConfig.custom()
                .setConnectTimeout(timeout, TimeUnit.MILLISECONDS)
                .setSocketTimeout(timeout, TimeUnit.MILLISECONDS)
                .build());

        RequestConfig requestConfig = RequestConfig.custom()
                .setConnectionRequestTimeout(Timeout.ofMilliseconds(5000L))
                .build();

        final HttpGet request = new HttpGet("http://www.baidu.com:81");

        long start = System.currentTimeMillis();
        try (CloseableHttpClient client = HttpClientBuilder.create()
                .setDefaultRequestConfig(requestConfig)
                .setConnectionManager(connectionManager)
                .build();

             CloseableHttpResponse response = (CloseableHttpResponse) client
                     .execute(request, new CustomHttpClientResponseHandler())) {

            final int statusCode = response.getCode();
            log.info("{}", statusCode);
        } catch (Exception e) {
            long end = System.currentTimeMillis();
            long duration = end - start;
            log.error("time out duration: {}", duration);
            e.printStackTrace();
            return String.valueOf(duration);
        }
        return "success";
    }
}